<?php
/*
 *   This file is part of verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

function changeUserId($old,$new) {
	global $loader;
	global $_log;
	
	$sql = "UPDATE ".USERS." set userid = ".util::str(strtolower($new))." WHERE userid = ".util::str(strtolower($old));
	$_log->debug("USERS: ".$loader->execute($sql));
	
	$sql = "UPDATE ".UPLOAD." set userid = ".util::str(strtolower($new))." WHERE userid = ".util::str(strtolower($old));
	$_log->debug("UPLOAD: ".$loader->execute($sql));

	$sql = "UPDATE ".UPLOAD." set takeuser = ".util::str(strtolower($new))." WHERE takeuser = ".util::str(strtolower($old));
	$_log->debug("UPLOAD: ".$loader->execute($sql));

	$sql = "UPDATE ".GLOSSARY." set userid = ".util::str(strtolower($new))." WHERE userid = ".util::str(strtolower($old));
	$_log->debug("GLOSSARY: ".$loader->execute($sql));

	$sql = "UPDATE ".PREFERENCES." set userid = ".util::str(strtolower($new))." WHERE userid = ".util::str(strtolower($old));
	$_log->debug("PREFERENCES: ".$loader->execute($sql));
				
	$sql = "SHOW TABLES LIKE '".FILES."%'";
	$list = $loader->fetchlist($sql);
	
	foreach ($list as $row) {
		$sql = "UPDATE ".$row[0]." set userid = ".util::str(strtolower($new))." WHERE userid = ".util::str(strtolower($old));
		$_log->debug($row[0].": ".$loader->execute($sql));
		$sql = "UPDATE ".$row[0]." set userid2 = ".util::str(strtolower($new))." WHERE userid2 = ".util::str(strtolower($old));
		$_log->debug($row[0].": ".$loader->execute($sql));
	}
	
	$sql = "SHOW TABLES LIKE '".HISTORY."%'";
	$list = $loader->fetchlist($sql);
	
	foreach ($list as $row) {
		$sql = "UPDATE ".$row[0]." set userid = ".util::str(strtolower($new))." WHERE userid = ".util::str(strtolower($old));
		$_log->debug($row[0].": ".$loader->execute($sql));
	}
	
}

if ($op=="save") {
	if ($CURRENT_USER->groupid==GROUP_ADMIN) {
		if ($param['userid']!=$param['ouserid']) {
			$_log->debug("Change userid from ".$param['ouserid']." to ".$param['userid']);
			changeUserId($param['ouserid'],$param['userid']);
		}
	}
	$u = new user;
	$u->userid=$param['userid'];
	$loader->get($u);
	$u->groupid=$param['groupid'];
	if ($CURRENT_USER->groupid==GROUP_ADMIN) {
		$u->teamid=$param['teamid'];	
	}
	$loader->update($u);
	success();
} else if ($op=="toggle") {
	$ids = stripslashes($param['ids']);
	$idlist = json_decode($ids);
	$_log->debug($idlist);
	
	foreach ($idlist as $uid) {
		$u = new user;
		$u->userid=$uid;
		$loader->get($u);
		if ($u->status==STATUS_ACTIVE) {
			$u->status=STATUS_INACTIVE;
		} else {
			$u->status=STATUS_ACTIVE;
		}
		$loader->update($u);
	}
	success();
} else if ($op=="load") {
	include(ROOT_PATH."/form/user_admin_form.php");	
} else if ($op=="new") {
	$ti = new temporal;
	$ti->id=util::encrypt($param['mail'].CODE_REQUEST.getTeamId($CURRENT_USER));
	$ti->options=CODE_REQUEST;
	$ti->extra=$param['mail'];
	$ti->expire=util::now();
	$ti->teamid = getTeamId($CURRENT_USER);
	
	$loader->save($ti);
	
	$t = new temporal;
	$t->id = util::hashcode($param['mail']);
	$t->options = CODE_USER_REG;
	$t->extra=$param['mail'];
	$t->teamid = getTeamId($CURRENT_USER);
	$t->filler=$ti->id;
	
	$loader->save($t);
	
	$team = new team;
	$team->id = getTeamId($CURRENT_USER);
	$loader->get($team);
	
	mailInviteUser($t->extra,$team->name,$t->id,$t->expire,strtolower($team->lang));
	success();	
} else if ($op=="newm") {
	$ids = stripslashes($param['ids']);
	$idlist = json_decode($ids);
	foreach ($idlist as $uid) {
		$t = new temporal;
		$t->id = $uid;
		if ($loader->getBy($t,$t->get(false))) {
			$t1 = new temporal;
			$t1->id = util::encrypt($t->id);
			$loader->delete($t1);
			
			$t1->options = CODE_USER_REG;
			$t1->extra=$t->extra;
			if ($t->teamid=="0") {
				$t1->teamid = getTeamId($CURRENT_USER);
			} else {
				$t1->teamid = $t->teamid;
			}
			$t1->filler=$t->id;
			$loader->save($t1);
			
			$team = new team;
			$team->id = $t1->teamid;
			$loader->get($team);
			
			mailInviteUser($t1->extra,$team->name,$t1->id,$t1->expire);			
		}
	}
	success();
} else if ($op=="rmr") {
	$ids = stripslashes($param['ids']);
	$idlist = json_decode($ids);
	foreach ($idlist as $uid) {
		$t = new temporal;
		$t->id = $uid;
		$loader->delete($t);
	}
	success();
} else if ($op=="rpwd") {
	$ids = stripslashes($param['ids']);
	$idlist = json_decode($ids);
	$npwd = util::createPassword(MIN_PASSWORD_LEN); 
	foreach ($idlist as $uid) {
		$u = new user;
		$u->userid=$uid;
		if ($loader->get($u)) {
			$u->passcode = $npwd;
			$u->passexpire = util::now();
			$loader->update($u);		
		}
	}
	success(String::format(s("New password: {0}"),$npwd));
} else if ($op=="accreq") {
	$tn = new temporal;
	$tn->options="req";
	if ($CURRENT_USER->groupid==GROUP_ADMIN) {
		$tn->teamid=null;	
	} else {
		$tn->teamid=getTeamId($CURRENT_USER);
	}
	$list = $loader->loadListBy($tn,$tn->listBy());
	$rlist = array();
	foreach ($list as $t) {
		$t1 = new temporal;
		if ($t->teamid!="0") {
			$t1->teamid=$t->teamid;	
		}
		$t1->filler=$t->id;
		$t1->options=CODE_USER_REG;
		if ($loader->getBy($t1,$t1->listBy())) {
			if (util::now()<util::mysqlToDate('YmdHis',$t1->expire)) {
				$m = s("Sent");
				$s = 1;
			} else {
				$m = s("Expired");
				$s = 0;
			}
		} else {
			$m=s("Pending");
			$s=0;
		}
		$tn = "N/A";
		if ($t->teamid!="0") {
			$team = new team;
			$team->id=$t->teamid;
			if ($loader->get($team)) {
				$tn = $team->name;
			}
		}
		$team = new team;
		$team->id=$t->teamid;
		/*
		$t->obj_array['status']=$s;
		$t->obj_array['statusmsg']=$m;
		$t->obj_array['teamname']=$tn;
		*/
		$t->status=$s;
		$t->statusmsg=$m;
		$t->teamname=$tn;		
		$rlist[]=$t;
	}
	out(jsonList($rlist,array("id","extra","teamid","status","statusmsg","expire","teamname")));
} else {
/*
	if (check("USER_ADMIN_LAST_LOGIN")) {
		$lastlogin = retrieve("USER_ADMIN_LAST_LOGIN");
	} else {
		$lastlogin = String::toString(($loader->count(credentials::exists($CURRENT_USER->groupid,"user_admin_super"))));
		cache("USER_ADMIN_LAST_LOGIN",$lastlogin);
	}
*/
	$us = new user;
	if ($CURRENT_USER->groupid != GROUP_ADMIN) {
		$us->userid=$CURRENT_USER->userid;
		$us->teamid=getTeamId($CURRENT_USER);
		$userlist = $loader->loadListBy($us,$us->listByFilters(null,null,true));
	} else {
		$us->userid=$CURRENT_USER->userid;
		$userlist = $loader->loadList($us);
	}
	/*
	if ($lastlogin=="0") {
		out(jsonList($userlist,array("userid","name","groupid","teamid","status","teamname")));
	} else {*/
		out(jsonList($userlist,array("userid","name","groupid","teamid","status","teamname","lastlogin")));
	//}
	
}
?>